package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.requests.BaseRequst;
import com.example.springboot.controller.requests.LoginRequest;
import com.example.springboot.controller.requests.changePassRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.IAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class AdminService implements IAdminService {
    @Autowired
    AdminMapper userMapper;
    public static String defatpass="123";
    @Override
    public List<Admin> list() {

        return userMapper.list();
    }
    @Override
    public PageInfo<Admin> page(BaseRequst userPageRequst)
    {

        PageHelper.startPage(userPageRequst.getPageNum(),
                userPageRequst.getPageSize());//前端传入
        List<Admin>  users=userMapper.listBycondition(userPageRequst);
       return new PageInfo<>(users);

    }

    @Override
    public void save(Admin user) {
        //当作卡号
        Date date=new Date();
        if (user.getPassword()==null)
        {
            user.setPassword(defatpass);
            user.setPassword(SecureUtil.md5(user.getPassword()).substring(0,30));

        }
//        user.setUsername(DateUtil.format(date,"yyyyMMdd")+Math.abs(IdUtil.simpleUUID().hashCode()));
        userMapper.save(user);
    }

    @Override
    public Admin getByid(Integer id) {
return userMapper.getByid(id);
    }

    @Override
    public void update(Admin user) {
//        Date date=new Date();
//        user.setUsername(DateUtil.format(date,"yyyyMMdd")+IdUtil.simpleUUID());
          user.setUpdatetime(new Date());
        userMapper.updateById(user);

    }

    @Override
    public void delete(Integer id) {
        userMapper.deleteByid(id);
    }

    @Override

    public LoginDTO login(LoginRequest loginRequest) {


            Admin admin=  userMapper.getByUsernameAndPassword(loginRequest);
            if (admin==null)
            {
                throw new ServiceException("用户名或密码错误");
            }
            LoginDTO  loginDTO=new LoginDTO();
            BeanUtils.copyProperties(admin,loginDTO);
            return loginDTO;



    }

    @Override
    public void savepass(changePassRequest changePassRequest) {
        Admin user= userMapper.getByid(changePassRequest.getID());
        user.setPassword(changePassRequest.getNewpass());

        userMapper.updatePass(user);
    }

}
